package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.StudentInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/8/1.
 */
public class RecommendFriendAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();
    private List<String> userIdList = new ArrayList<String>();

    public RecommendFriendAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.recommendfriend_item, null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_icon);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        final EatShowUserInfo info = mList.get(position);

        if (info != null) {
            String headUrl = info.getUserLogo();
            if (headUrl != null && headUrl.length() > 0) {
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
                String cache_key = ImageCache.getInstance(mContext).getCacheKey(headUrl, size);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cache_key);
                if (cacheBitmap != null) {
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.headImg.setTag(headUrl);
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headUrl, size);
                }
            } else {
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }

            String nickname = info.getNickName();
            if (nickname != null) {
                try {
                    nickname = URLDecoder.decode(nickname, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            childHolder.nickNameText.setText(nickname);
            childHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (!userIdList.contains(info.getUserId())) {
                            userIdList.add(info.getUserId());
                        }
                    } else {
                        if (userIdList.contains(info.getUserId())) {
                            userIdList.remove(info.getUserId());
                        }
                    }
                }
            });
            childHolder.mCheckBox.setChecked(true);
            final ChildHolder finalChildHolder = childHolder;
            childHolder.headImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalChildHolder.mCheckBox.isChecked()) {
                        finalChildHolder.mCheckBox.setChecked(false);
                    } else {
                        finalChildHolder.mCheckBox.setChecked(true);
                    }
                }
            });
            if (!userIdList.contains(info.getUserId())){
                userIdList.add(info.getUserId());
            }
        }
        return convertView;
    }

    public List<String> getCheckedUser() {
        return userIdList;
    }

    public void cleanCheckedUser(){
        userIdList.clear();
    }

    class ChildHolder {
        ImageView headImg;
        TextView nickNameText;
        CheckBox mCheckBox;
    }
}
